package com.ctrl.riveradministrator.mvp.view.activity.river_doc;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BaseFragment;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.mvp.model.RiverInfo;
import com.ctrl.riveradministrator.mvp.view.activity.river_info.RiverInfoActivity;
import com.ctrl.riveradministrator.mvp.view.activity.river_info.RiverInfoFragment;
import com.ctrl.riveradministrator.mvp.view.fragment.RiverFragment;

import java.util.ArrayList;
import java.util.List;

public class RiverDocActivity extends BaseActivity implements TabLayout.OnTabSelectedListener {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private DrawerLayout drawerLayout;

    private TabLayout tab;
    private ViewPager content;
    private List<Fragment> fragments;
    private ContentPagerAdapter contentPagerAdapter;

    RiverInfoFragment riverInfoFragment = RiverInfoFragment.newInstance();
    OutfallsFragment outfallsFragment = OutfallsFragment.newInstance();
    PaiFragment paiFragment = PaiFragment.newInstance();

    public static RiverInfo riverInfo;

    @Override
    protected void initParams(Bundle parms) {
        riverInfo = (RiverInfo) getIntent().getSerializableExtra("river_info");
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_river_doc;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = (Toolbar) $(R.id.toolbar);
        titleTv = (TextView) $(R.id.title_tv);
        leftMenu = (ImageView) $(R.id.left_iv);
        tab = (TabLayout) $(R.id.tab);
        content = (ViewPager) $(R.id.content);
        //drawerLayout = findViewById(R.id.drawer);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText("一河一档");
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));

        initContent();
    }

    @Override
    protected void doBusiness(Context mContext) {

    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        tab.addOnTabSelectedListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    /**
     * 初始化tab内容
     */
    private void initContent() {
        Fragment[] tabFragment = {riverInfoFragment, outfallsFragment};
        String tabTitles[] = new String[]{"河流自然信息", "排污口管理信息"};
        contentPagerAdapter = new ContentPagerAdapter(getSupportFragmentManager(), tabTitles, tabFragment);
        for (int i = 0; i < tabTitles.length; i++) {
            tab.addTab(tab.newTab().setText(tabTitles[i]));
        }
        content.setAdapter(contentPagerAdapter);
        tab.setupWithViewPager(content);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
       /* switch (tab.getPosition()) {
            case 0:
                switchFragment(riverInfoFragment, R.id.content);
                break;
            case 1:
                switchFragment(paiFragment, R.id.content);
                break;
            case 2:
                switchFragment(outfallsFragment, R.id.content);
                break;
        }*/
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    private class ContentPagerAdapter extends FragmentPagerAdapter {

        public Fragment tabFrames[];
        public String titles[];

        public ContentPagerAdapter(FragmentManager fm, String[] titles, Fragment[] tabFrames) {
            super(fm);
            this.titles = titles;
            this.tabFrames = tabFrames;
        }

        @Override
        public Fragment getItem(int i) {
            return tabFrames[i];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];

        }
    }
}